package com.djx.监听;

import com.djx.基础.MyZookepper01;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.awt.print.Pageable;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;


/**
 * https://www.bilibili.com/video/BV1M741137qY?p=32
 */
public class exists事件监听 {
//    ZooKeeper zooKeeper = new MyZookepper01().zooKeeper;

    private String myIP = "192.168.15.132:2181";
    ZooKeeper zooKeeper;

    @Before
    public void exists() throws IOException, InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(myIP, 5000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    countDownLatch.countDown();
                }
                System.out.println("path : " + watchedEvent.getPath());
                System.out.println("eventType : " + watchedEvent.getType());
            }
        });
        countDownLatch.await();
    }


    @Test
    public void watcherExists1() throws KeeperException, InterruptedException {
        //arg1 :节点路径
        //arg2 : 使用连接对象中的watecher
        zooKeeper.exists("/zzz", true);
        Thread.sleep(50000);//用于监听
        System.out.println("结束");
    }

    /**
     * 一次性的
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void watcherExists2() throws KeeperException, InterruptedException {
        //arg1 :节点路径


        //arg2 : 使用连接对象中的watecher
        zooKeeper.exists("/zzz", new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("path : " + watchedEvent.getPath());
                System.out.println("eventType : " + watchedEvent.getType());
            }
        });
        Thread.sleep(50000);//用于监听
        System.out.println("结束");
    }

    /**
     * 在时间节点内 无限制监听
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void watcherExists3() throws KeeperException, InterruptedException {
        //arg1 :节点路径
        //arg2 : 使用连接对象中的watecher
        zooKeeper.exists("/zzz", new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("path : " + watchedEvent.getPath());
                System.out.println("eventType : " + watchedEvent.getType());
                try {
                    zooKeeper.exists("/zzz", this);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread.sleep(50000);//用于监听
        System.out.println("结束");
    }

    /**
     * 在时间节点内 多个监听器的注册
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void watcherExists4() throws KeeperException, InterruptedException {
        //arg1 :节点路径
        //arg2 : 使用连接对象中的watecher
        zooKeeper.exists("/zzz", new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("path : " + watchedEvent.getPath());
                System.out.println("eventType : " + watchedEvent.getType());
                try {
                    zooKeeper.exists("/zzz", this);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        zooKeeper.exists("/yyy", new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                System.out.println("path : " + watchedEvent.getPath());
                System.out.println("eventType : " + watchedEvent.getType());
                try {
                    zooKeeper.exists("/zzz", this);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread.sleep(500000);//用于监听
        System.out.println("结束");
    }
}
